package com.szamc.tableroundapi.controller;

import com.szamc.tableroundapi.model.Request.BaseTableRoundRequest;
import com.szamc.tableroundapi.model.Request.MultiTableRoundRequest;
import com.szamc.tableroundapi.model.Request.RecursionTableRequest;
import com.szamc.tableroundapi.model.Response.Common.ResultList;
import com.szamc.tableroundapi.service.BaseTableRoundApiService;
import com.szamc.tableroundapi.service.MultiTableRoundApiService;
import com.szamc.tableroundapi.service.TreeTableApiService;
import lombok.var;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/Table")
public class TableController {
    @PostMapping("/BaseTableRound")
    public ResultList<List<LinkedHashMap>> BaseTableRound(@RequestBody BaseTableRoundRequest request) throws NoSuchFieldException, IllegalAccessException {
        var service = new BaseTableRoundApiService();
        var result = service.GetRoundTable(
                request.tableList,
                request.baseTableOptions,
                request.unitOption.divideNumber,
                request.unitOption.precision);
        return ResultList.Success(result);
    }

    @PostMapping("/MultiTableRound")
    public ResultList<List<LinkedHashMap>> MultiTableRound(@RequestBody MultiTableRoundRequest request) throws NoSuchFieldException, IllegalAccessException {
        var service = new MultiTableRoundApiService();
        var result = service.GetRoundTable(
                request.tableList,
                request.multiTableOption,
                request.unitOption.divideNumber,
                request.unitOption.precision);

        return ResultList.Success(result);
    }

    @PostMapping("/TreeTableRound")
    public  ResultList<List<ArrayList<Double>>>TreeTableRound(@RequestBody RecursionTableRequest request) throws NoSuchFieldException, IllegalAccessException {
        var _request = request;
        var _rowOptions = _request.recursionTableOption.rowOptions;
        var _columnOptions = _request.recursionTableOption.columnOptions;
        var _unitOption = _request.unitOption;
        var service = new TreeTableApiService();
        var result = service.GetRoundTable(request.tableList, _rowOptions, _columnOptions,_unitOption);
        return ResultList.Success(result);
    }
}
